package com.jinrustar.httpfileserver.springboot.controller;

import com.jinrustar.httpfileserver.core.HttpFileServerApp;
import com.jinrustar.httpfileserver.core.http.HttpFileServer;
import com.jinrustar.httpfileserver.util.FileUtil;
import com.jinrustar.httpfileserver.util.WebUtils;
import com.jinrustar.httpfileserver.core.base.BaseController;
import com.jinrustar.httpfileserver.core.http.HttpFileServerHandler;
import com.jinrustar.httpfileserver.core.util.WebUtil;
import com.jinrustar.httpfileserver.springboot.annatation.LoginRequired;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "file", description = "file")
@RequestMapping(value = "/file", method = {RequestMethod.GET, RequestMethod.POST})
public class FileController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(HttpFileServerHandler.class);


    // u=default&a=x&i=820491902842318848&ip=183.16.190.209&_s=ae26e1c3c24e07696afeb6a2a692f80f
    @RequestMapping(value = "/list")
    public Object listFiles(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response, HttpSession httpSession,
                            @RequestParam(value = "path", required = false, defaultValue = "") String path,
                            @RequestParam(value = "u", required = false, defaultValue = "default") String userName,
                            @RequestParam(value = "a", required = false, defaultValue = "") String avatar,
                            @RequestParam(value = "i", required = false, defaultValue = "") String id,
                            @RequestParam(value = "ip", required = false, defaultValue = "0.0.0.0") String ip
            /*@RequestParam(value = "_s", required = false, defaultValue = "") String sign*/
                            /*java.lang.IllegalArgumentException: RFC6265 Cookie values may not contain character: [ ]
	at org.eclipse.jetty.http.Syntax.requireValidRFC6265CookieValue(Syntax.java:136)*/
    ) throws IOException {

        /*path = path.contains("%") ? URLDecoder.decode(path) : path;*/

        if (!HttpFileServerApp.exists(path)) {
            logger.info("##listfiles:{},文件/目录不存在", path);
            return setSuccessModelMap(modelMap, "404," + path);
        }

        if (HttpFileServerApp.isFile(path)) {
            /*重定向到netty http file server 下载文件(客户端浏览器自己处理重定向：重新发起请求)*/
            String queryString = request.getQueryString();
            response.sendRedirect(HttpFileServer.ipAndPort + "?" + queryString);
            return null;
        }

        /*文件列表*/
        logger.info("###ip:{}", WebUtil.getHost(request));
        return setSuccessModelMap(modelMap, HttpFileServerApp.listFiles(path));
    }
}
